/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from "vue";
import Router from "vue-router";
import { getUrlKey, getToken, setToken } from "@/service/TokenUtils";
import devRoutes from "views/all.routes.js";
console.log('devRoutes :>> ', devRoutes);
Vue.use(Router);

// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [...devRoutes];

const router = new Router({
  mode: "hash",
  scrollBehavior: () => ({ y: 0 }),
  routes: globalRoutes,
});

// TODO 如果没登陆直接进入就跳到portal的登录页面
router.beforeEach((to, from, next) => {
  if (to.path === "/login" || to.path === "/404") return next();
  let token = getUrlKey("token");
  if (token && token !== "") {
    setToken(token);
  } else {
    token = getToken();
  }
  if (!token || !/\S/.test(token)) {
    //clearTokenModel()
    console.log("无访问权限");
    next({ name: "404" });
  } else {
    next();
  }
});

export default router;
